package cn.com.cifi.mars.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.constant.StageStatusEnum;
import cn.com.cifi.mars.entity.MdmStage;
import cn.com.cifi.mars.mapper.mars.StageMapper;
import cn.com.cifi.mars.service.StageService;

/**
 * <p>
 * （主数据）项目分期表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-24
 */
@Service
public class StageServiceImpl extends ServiceImpl<StageMapper, MdmStage> implements StageService {

	@Autowired
	StageMapper stageMapper;

	@Override
	public MdmStage selectById(String projectFid) {
		return stageMapper.selectByFId(projectFid, IsDeleteEnum.NO.getKey());
	}

	@Override
	public List<MdmStage> selectByProjectId(String projectId, int isDel) {
		QueryWrapper<MdmStage> groupWrapper = new QueryWrapper<MdmStage>();
		groupWrapper.eq("project_id", projectId).eq("is_delete", isDel).last(" GROUP BY project_fid");
		return stageMapper.selectList(groupWrapper);
	}

	@Override
	public List<MdmStage> selectStagesByprojectFid(String projectFid) {
		return stageMapper.selectStagesByprojectFid(projectFid);
	}

	@Override
	@Transactional
	public void updateByProjectFid(MdmStage stage) {
		stageMapper.updateByProjectFid(stage);
	}

	@Override
	public void insertBatch(List<MdmStage> mdmStageList) {
		stageMapper.insertBatch(mdmStageList);
	}

	@Override
	public MdmStage selectByKingdeeProjectfId(String kingdeeValueStageId) {
		QueryWrapper<MdmStage> groupWrapper = new QueryWrapper<MdmStage>();
		groupWrapper.eq("kingdee_projectf_id", kingdeeValueStageId);
		return stageMapper.selectOne(groupWrapper);
	}

	@Override
	public boolean isActiveStage(String projectFid) {
		MdmStage stage = stageMapper.selectByFId(projectFid, IsDeleteEnum.NO.getKey());
		 if(stage != null){
			String status = stage.getStatus();
            if (StringUtils.isNotEmpty(status) && status.equals(StageStatusEnum.CTIVE.getKey())){
               return true;
            }
	     }
		return false;
	}

	@Override
	public List<MdmStage> selectAll() {
		QueryWrapper<MdmStage> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_delete",IsDeleteEnum.NO.getKey());
        return stageMapper.selectList(queryWrapper);
	}
}
